package com.zone.sdk.module.account.http;

import android.util.Log;

import com.zone.sdk.common.http.Content;
import com.zone.sdk.common.http.OkHttpManager;
import com.zone.sdk.common.http.apimock.ApiMock;
import com.zone.sdk.common.http.apimock.fastjson.FastJsonConverterFactory;
import com.zone.sdk.common.http.apimock.interceptor.MockInterceptor;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * @Author: pkqlqx
 * @CreateDate: 2021/12/5 14:30
 * @Description:
 */
public class AccountOkHttpManager extends OkHttpManager {

    private static AccountOkHttpManager instance;

    private static HeaderInterceptor headerInterceptor;

    private static BodyInterceptor bodyInterceptor;

    private static Map<String, String> header = new HashMap<>();
    private static Map<String, String> body = new HashMap<>();

    protected OkHttpClient mOkHttpClient;
    protected Retrofit mRetrofit;
    private final  String baseUrl;

    private AccountOkHttpManager(String baseUrl){
        super();
        headerInterceptor = new HeaderInterceptor();
        bodyInterceptor = new BodyInterceptor();
        this.baseUrl = baseUrl;
    }

    public synchronized static AccountOkHttpManager getInstance(String baseUrl){
        if (instance == null){
            instance = new AccountOkHttpManager(baseUrl);
        }
        return instance;
    }

    public void setAuthorization(String authorization) {
        header.put(Content.AUTHORIZATION_ACCOUNT, authorization);
    }

    public void setHeader(String key, String value){
        header.put(key, value);
    }

    @Override
    public <T> T build(Class<T> service) {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder()
                .connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS)
                .readTimeout(READ_TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(WRITE_TIME_OUT, TimeUnit.SECONDS);
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        if (ApiMock.isMock()){
            builder.addInterceptor(new MockInterceptor());
        }else {
            builder.addInterceptor(loggingInterceptor);
            builder.addInterceptor(new Interceptor() {
                @Override
                public @NotNull Response intercept(@NotNull Chain chain) throws IOException {
                    Request.Builder builder1 = chain.request().newBuilder();
                    if (header.size() > 0){
                        for (String key : header.keySet()) {
                            builder1.addHeader(key, Objects.requireNonNull(header.get(key)));
                        }
                    }
//                    Request newRequest = chain.request().newBuilder()
//                            .addHeader("Authorization", "Bearer Bearer")
                    builder1.addHeader("timestamp", ""+System.currentTimeMillis());
                    builder1.addHeader("nonce", ""+System.currentTimeMillis());
                    builder1.addHeader("requestId", ""+System.currentTimeMillis());
//                            .addHeader("sign", "sign")
                    builder1.addHeader("x-device-from", "LSV0000000002");
                    builder1.addHeader("x-device-id", "x-device-id");
                    Log.d("Okhttp ", ""+builder1.toString());
                    return chain.proceed(builder1.build());
                }
            });
//            builder.addInterceptor(bodyInterceptor);
        }
        mOkHttpClient = builder.build();

        mRetrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(FastJsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(mOkHttpClient)
                .build();
        return mRetrofit.create(service);
    }
}
